package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.consts.Const;
import com.example.demo.entity.Comment;
import com.example.demo.entity.Examine;
import com.example.demo.entity.Moment;
import com.example.demo.entity.Poetry;
import com.example.demo.mapper.CommentMapper;
import com.example.demo.mapper.ExamineMapper;
import com.example.demo.mapper.MomentMapper;
import com.example.demo.param.ExamineParam;
import com.example.demo.service.ExamineService;
import com.example.demo.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ExamineServiceImpl implements ExamineService {

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private MomentMapper momentMapper;

    @Resource
    private ExamineMapper examineMapper;

    @Override
    public Result addObject(Long id, Integer type) {
        if (type == 0) {
            Moment moment = momentMapper.selectById(id);
            Examine examine = Examine.builder().objectId(id).type(type).content(moment.getContent()).img(moment.getImg()).build();
            int insert = examineMapper.insert(examine);
            if (insert > 0) {
                return Result.ok();
            }
            return Result.error("插入失败");
        } else if (type == 1 || type == 2) {
            Comment comment = commentMapper.selectById(id);
            Examine examine = Examine.builder().objectId(id).type(type).content(comment.getContent()).build();
            int insert = examineMapper.insert(examine);
            if (insert > 0) {
                return Result.ok();
            }
            return Result.error("插入失败");
        } else {
            return Result.error("输入类型错误");
        }
    }

    @Override
    public List<Examine> selectObject(Integer pageNo) {
        QueryWrapper<Examine> wrapper = new QueryWrapper();
        //随机生成 10个id去查库
        wrapper.orderByDesc("create_time");
        Page<Examine> page = new Page<>(pageNo, Const.PAGE_SIZE);
        Page<Examine> examinePage = examineMapper.selectPage(page, wrapper);
        return examinePage.getRecords();
    }

    @Override
    public Result examine(ExamineParam examineParam) {
        Boolean flag = examineParam.getFlag();
        Integer type = examineParam.getType();
        Long id = examineParam.getId();
        Long objectId = examineParam.getObjectId();
        Examine examine = new Examine(id,flag,true);
        if (flag == false && type == 0){
            examineMapper.updateById(examine);
            momentMapper.deleteById(objectId);
        }else if (flag == false && (type == 1 || type ==2)){
            examineMapper.updateById(examine);
            commentMapper.deleteById(id);
        }else {
            examineMapper.updateById(examine);
        }
        return Result.ok();
    }
}
